package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import top.xiaoshuaila.moments.pojo.Dynamic;

import java.sql.Timestamp;
import java.util.List;

@Repository
public interface DynamicMapper {
    @Select("select \n" +
            "  a.`id` id,\n" +
            "  a.`userid` userid,\n" +
            "  a.`time` time,\n" +
            "  a.`iszhuan` iszhuan,\n" +
            "  a.`dyid` dyid,\n" +
            "  a.`selfonly` selfonly,\n" +
            "  b.`text` text,\n" +
            "  b.`title` title,\n" +
            "  b.`image` image,\n" +
            "  b.`userid` dyuserid,\n" +
            "  c.num zan,\n" +
            "  d.zhuan zhuan,\n" +
            "  e.`realname` realname,\n" +
            "  (select count(*)>0 from zan where zan.userid=${id} and zan.dyid=a.id) iszan\n" +
            "from\n" +
            "  userdy a \n" +
            "  left join dynamic b \n" +
            "    on a.`dyid` = b.`id` \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*) as num \n" +
            "    from\n" +
            "      zan \n" +
            "    group by zan.`dyid`) c \n" +
            "    on a.id = c.dyid \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*)-1 as zhuan \n" +
            "    from\n" +
            "      userdy \n" +
            "    group by userdy.`dyid`) d \n" +
            "    on b.id = d.dyid \n" +
            "  left join \n" +
            "    users e \n" +
            "    on b.`userid` = e.`id` \n" +
            "where a.userid = ${id} order by a.time desc limit ${page},${num}")
    List<Dynamic> getDynamicById(long id,int page,int num);

    @Insert("insert into dynamic (id,text,time,image,title,userid) values ('${id}','${text}','${time}','${image}','${title}',${userid})")
    void adddynamic(String id, String text, Timestamp time, String image, String title,
                    long userid);
    @Insert("insert into userdy (userid,time,iszhuan,dyid,selfonly) values (${userid},'${time}',${iszhuan},'${dyid}',${selfonly})")
    void adduserdy(long userid, Timestamp time, boolean iszhuan, String dyid, boolean selfonly);
    @Select("select \n" +
            "  count(*)\n" +
            "from\n" +
            "  userdy a\n" +
            "where a.userid = ${id}")
    int gettotal(long id);
}
